package common

import (
	"common/jwt"
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

// CheckToken 验证token
// @Summary 验证token
// @Description 验证token
// @Tags 通用
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocCheckToken false "验证token"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /common/checkToken [post]
func (CHandler) CheckToken(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.CheckTokenReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("User:Login with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("User:Login with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	_, err := jwt.ParseToken(req.Token)
	if err != nil {
		resp.Data = false
		resp.SendCode(c, models.CodeInvalidToken)
		return
	}
	resp.Data = true
	resp.SendSuccess(c)
	return
}
